package org.jsola.hr;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jsola.user.lock.EnableWriteLock;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static org.jsola.hr.constant.HrConstants.SQL_SESSION_FACTORY_BEAN_NAME;

/**
 * @author zhr
 */
@SpringBootApplication
@ComponentScan("org.jsola")
@EnableScheduling
@EnableAsync
@EnableWriteLock
public class HrApplication {

    @Autowired
    private MybatisProperties mybatisProperties;

    public static void main(String[] args) {
        new SpringApplicationBuilder(HrApplication.class)
                .run(args);
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier(SQL_SESSION_FACTORY_BEAN_NAME) SqlSessionFactory sqlSessionFactory) {
        ExecutorType executorType = this.mybatisProperties.getExecutorType();
        if (executorType != null) {
            return new SqlSessionTemplate(sqlSessionFactory, executorType);
        } else {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    @Configuration
    class MyWebMvcConfig implements WebMvcConfigurer {
        //@Bean
//        @Profile({"test"})
//        public MyWebMvcConfig getMyWebMvcConfig() {
//            return new MyWebMvcConfig() {
//                @Override
//                public void addInterceptors(InterceptorRegistry registry) {
//                    registry.addInterceptor(new HandlerInterceptor() {
//                        private static final String SITE_ID = "3040F4DB06FC4D64BC6EF7EEC4B6C402";
//                        private static final String CURR_USER_ID = "C24826CF51B14DF0BD6C12DA0C16035D";
//
////                        private static final String SITE_ID = "EAE7A6E4767345B284C2A443D729E2F0";
////                        private static final String CURR_USER_ID = "D934CBB12BA14EAD9F45077CD4661425";
//
//                        @Override
//                        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//                            TokenUser tokenUser = new TokenUser();
//                            tokenUser.setDeviceType("web");
//                            tokenUser.setLoginType(1);
//                            tokenUser.setSiteId(SITE_ID);
//                            tokenUser.setUserId(CURR_USER_ID);
//                            tokenUser.setUserName("张三");
//                            tokenUser.setSiteName("test");
//                            System.out.println("token：" + JSON.toJSONString(tokenUser));
//                            SecurityContext.setTokenData(tokenUser);
//                            return true;
//                        }
//                    }).addPathPatterns("/**");
//                }
//            };
//        }
    }

}

